package com.tian.print;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.postek.cdf.CDFPTKAndroid;
import com.postek.cdf.CDFPTKAndroidImpl;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PrintDialogActivity extends AppCompatActivity {
    private static final int REQUEST_CODE = 10000;
    private int ConnectOrClose = 0;

    @BindView(R.id.et_wifi_ip1)
    EditText et_ip1;

    @BindView(R.id.et_wifi_ip2)
    EditText et_ip2;

    @BindView(R.id.et_wifi_ip3)
    EditText et_ip3;

    @BindView(R.id.et_wifi_ip4)
    EditText et_ip4;

    @BindView(R.id.et_wifi_port)
    EditText et_port;

    @BindView(R.id.btn_wifi_connectandclose)
    Button btnConnectAndClose;

    private CDFPTKAndroid cdf = null;

    public static final int CONNECT_TRUE = 100112;
    public static final int CONNECT_FAIRED = 100118;

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            if (msg.what == CDFPTKAndroid.PTK_MSG_WHAT_WIFICONNECT) {
                if (msg.arg1 == 0) {
                    ConnectOrClose = 1;
                    btnConnectAndClose.setText(com.postek.coyote.postekwifi.R.string.disconnect);
                    Toast.makeText(PrintDialogActivity.this, com.postek.coyote.postekwifi.R.string.connect_success, Toast.LENGTH_SHORT).show();
                    setResult(CONNECT_TRUE);
                    finish();
                } else {
                    Toast.makeText(PrintDialogActivity.this, com.postek.coyote.postekwifi.R.string.disconnect_wifi, Toast.LENGTH_SHORT).show();
                    setResult(CONNECT_FAIRED);
                    finish();
                }
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_print_dialog);
        ButterKnife.bind(this);

        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads().detectDiskWrites().detectNetwork()
                .penaltyLog().build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
                .penaltyLog().penaltyDeath().build());

        cdf = new CDFPTKAndroidImpl(this, handler);
    }

    @OnClick(R.id.btn_wifi_connectandclose)
    public void onClick(View v) {
        int i = v.getId();
        if (i == com.postek.coyote.postekwifi.R.id.btn_wifi_connectandclose) {
            if (ConnectOrClose == 0) {
                String ip = et_ip1.getText().toString() + "." + et_ip2.getText().toString() + "." + et_ip3.getText().toString() + "." + et_ip4.getText().toString();
                int port = Integer.parseInt(et_port.getText().toString());
                cdf.PTK_ConnectWiFi(ip, port);
            }
            if (ConnectOrClose == 1) {
                cdf.PTK_CloseConnectWiFi();
                ConnectOrClose = 0;
                btnConnectAndClose.setText(com.postek.coyote.postekwifi.R.string.connect);
                Toast.makeText(PrintDialogActivity.this, com.postek.coyote.postekwifi.R.string.unConnect, Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);

    }
}
